package com.cal.utms.dao.maintenance;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.InstrumentType;
import com.cal.utms.enumType.InterestRate;
import com.cal.utms.enumType.InterestType;
import com.cal.utms.enumType.WithholdingTax;
import com.cal.utms.util.HibernateUtil;

public class InstrumentTypeDao {

	public InstrumentTypeDao() {

	}

	public Object addInstrumentType(InstrumentType instrumentType) {
		Object obj;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.save(instrumentType);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			obj = new Object();
			System.out.println("Exception save InsType " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;
	}

	public void updateInstrumentType(InstrumentType instrumentType) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(instrumentType);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			System.out.println("Exception Update InsType, ID: " + instrumentType.getId() + " :" + ex.getMessage());
			ex.printStackTrace();
		} finally {
			session.close();
		}
	}

	public InstrumentType getById(Integer id) {
		Session session = HibernateUtil.getSession().openSession();
		InstrumentType instrumentType = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			instrumentType = (InstrumentType) session.get(InstrumentType.class, id);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getByID InsType " + e.getMessage());
		} finally {
			session.close();
		}
		return instrumentType;
	}

	@SuppressWarnings("unchecked")
	public List<InstrumentType> getInstrumentTypeList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<InstrumentType> instrumentTypeList = null;
		try {
			tx = session.beginTransaction();
			instrumentTypeList = session.createQuery("from InstrumentType").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getInstrumentTypeList " + e.getMessage());
		} finally {
			session.close();
		}
		return instrumentTypeList;
	}

	@SuppressWarnings("unchecked")
	public List<InstrumentType> getInstrumentTypeList(String querry) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<InstrumentType> instrumentTypeList = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from InstrumentType where upper(instrumentName) like :name");
			query.setParameter("name", querry.toUpperCase() + "%");
			instrumentTypeList = query.list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getInstrumentTypeList " + e.getMessage());
		} finally {
			session.close();
		}
		return instrumentTypeList;
	}

	@SuppressWarnings("unchecked")
	public InstrumentType getInstrumentTypeByName(String name) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		InstrumentType instrumentType = null;
		List<InstrumentType> instrumentTypeList = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from InstrumentType where instrumentName = :name");
			query.setParameter("name", name);
			instrumentTypeList = query.list();
			if (instrumentTypeList.size() > 0) {
				instrumentType = instrumentTypeList.get(0);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getInstrumentTypeList " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return instrumentType;
	}

	@SuppressWarnings("unchecked")
	public InstrumentType getInstumentByfactors(com.cal.utms.enumType.InstrumentType instrumentType, int cuponFrequency, String dayBasis) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		InstrumentType instrumentType2 = null;
		List<InstrumentType> instrumentTypeList = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from InstrumentType I where I.instrumentType=:instrumentType and I.cuponFrequency=:cuoFr and I.dayBasis=:dayBasis and I.listed=1");
			query.setParameter("instrumentType", instrumentType).setParameter("cuoFr", cuponFrequency).setParameter("dayBasis", dayBasis);
			instrumentTypeList = query.list();
			if (instrumentTypeList.size() > 0) {
				instrumentType2 = instrumentTypeList.get(0);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getInstrumentTypeList " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return instrumentType2;
	}

	@SuppressWarnings("unchecked")
	public com.cal.utms.entity.InstrumentType getInstumentByAllfactors(com.cal.utms.enumType.InstrumentType instrumentType, byte script, byte denominals, byte listed,
			WithholdingTax n, InterestRate interestRate, InterestType s, String Daybasis, byte coupon, int couFreQ, byte shortmatu, byte calculation) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		InstrumentType instrumentType2 = null;
		List<InstrumentType> instrumentTypeList = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from InstrumentType I where I.instrumentType=:instrumentType and I.scripts=:script and I.denominals=:denominal and I.listed=:listed"
					+ " and I.withholdingTax=:withTax and I.interestRate=:interestRate and I.interestType=:interestType and I.dayBasis=:dayBasis"
					+ " and I.couponAvailability=:coupon and I.cuponFrequency=:cuoFr and I.shortMaturable=:short and I.calculationInput=:cal");
			query.setParameter("instrumentType", instrumentType).setParameter("script", script).setParameter("denominal", denominals).setParameter("listed", listed)
					.setParameter("withTax", n).setParameter("interestRate", interestRate).setParameter("interestType", s).setParameter("dayBasis", Daybasis)
					.setParameter("coupon", coupon).setParameter("cuoFr", couFreQ).setParameter("short", shortmatu).setParameter("cal", calculation);
			instrumentTypeList = query.list();
			if (instrumentTypeList.size() > 0) {
				instrumentType2 = instrumentTypeList.get(0);
			} else {
				instrumentType2 = null;
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println("Exception getInstrumentTypeList " + e.getMessage());
			e.printStackTrace();
		} finally {
			session.close();
		}
		return instrumentType2;
	}
}
